package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main4Activity extends AppCompatActivity {

    private ListView lv;
    private String[] titles = {"java","php","python","css","html"};
    private String[] contnet = {"11.11","11.12","11.13","11.14","11.15"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }
    private void initView(){

        lv = findViewById(R.id.listview);
        lv.setAdapter(new MyBaseAdapter());

    }
    private void initData(){

    }
    private void initEvent(){

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(Main4Activity.this, "点击了条目"+i, Toast.LENGTH_SHORT).show();
            }
        });

    }
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return titles[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder aa = null;
            if(aa == null){//当视图不存在时需要创建
                //加载布局
                view = View.inflate(Main4Activity.this,R.layout.item_layout,null);
                //初始化aa
                aa = new ViewHolder();
                //初始化控件
                aa.bc = view.findViewById(R.id.bc);
                aa.title =view.findViewById(R.id.it_tit_tv);
                aa.title1 =view.findViewById(R.id.it_tit_tv1);
                //在listview条目布局里缓存控件
                view.setTag(aa);
            }else{
                aa = (ViewHolder) view.getTag();
            }
            //如果已经在缓存中存在当前视图，则直接获取当前视图
            aa.title1.setText(contnet[i]+"");
            aa.title.setText(titles[i]+"");
            return view;
        }
    }
    class ViewHolder{
        ImageView bc;
        TextView title;
        TextView title1;
    }
}